package net.ibizsys.eclipse.studio.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.ibizsys.eclipse.studio.util.StringKit;
import net.ibizsys.eclipse.studio.xml.IBizApplication;
import net.ibizsys.eclipse.studio.xml.IBizEntity;
import net.ibizsys.eclipse.studio.xml.IBizFile;
import net.ibizsys.eclipse.studio.xml.IBizProject;
import net.ibizsys.eclipse.studio.xml.IBizSearchableNode;
import net.ibizsys.eclipse.studio.xml.IBizView;

import org.eclipse.jface.fieldassist.ContentProposal;
import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalProvider;

/**
 * 搜索框提示内容提供对象
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizSearchProposalProvider implements IContentProposalProvider
{
	private Map<String, IBizProject> projectMap = new HashMap<String, IBizProject>();

	public void update(Map<String, IBizProject> projectMap)
	{
		this.projectMap = projectMap;
	}

	@Override
	public IContentProposal[] getProposals(String content, int length)
	{
		List<ContentProposal> list = new ArrayList<ContentProposal>();

		if (content.trim().length() > 0)
		{
			String[] keywords = content.split("[ ]");

			for (IBizProject project : projectMap.values())
			{
				if (StringKit.IsEqual(project.getType(), IBizProject.TYPE_APP, true))
				{
					list.addAll(searchAPPProject(project, keywords));
				} else if (StringKit.IsEqual(project.getType(), IBizProject.TYPE_SRV, true))
				{
					list.addAll(searchSRVProject(project, keywords));
				}
			}
		}

		IContentProposal[] proposals = new IContentProposal[list.size()];
		list.toArray(proposals);

		return proposals;
	}

	/**
	 * 检索App类型项目
	 * 
	 * @param project
	 * @param keywords
	 * @return
	 */
	protected List<ContentProposal> searchAPPProject(IBizProject project, String[] keywords)
	{
		List<ContentProposal> list = new ArrayList<ContentProposal>();

		List<IBizView> views;

		if (keywords.length > 1)
		{
			List<IBizEntity> entities = project.getEntityList().match(keywords[0]);

			List<String> entityKeys = new ArrayList<String>();

			for (IBizEntity entity : entities)
			{
				entityKeys.add(entity.getId());
			}
			views = project.getViewList().match(entityKeys, keywords[1]);
		} else
		{
			views = project.getViewList().match(keywords[0]);
		}

		if (views != null)
		{
			for (IBizView view : views)
			{
				String name = view.getTitle();

				for (IBizFile file : view.getFileList())
				{
					// 判断是否有2个文件
					if (!StringKit.IsBlank(file.getUsrRealFilePath()))
					{
						list.add(new IBizSearchProposal("["+file.getProject().getCodeName()+"][USR]" + name + " " + file.getText(), file, false));
					}

					list.add(new IBizSearchProposal("["+file.getProject().getCodeName()+"][PUB]" + name + " " + file.getText(), file, true));
				}
			}
		}

		return list;
	}

	/**
	 * 检索SRV类型项目
	 * 
	 * @param project
	 * @param keywords
	 * @return
	 */
	protected List<ContentProposal> searchSRVProject(IBizProject project, String[] keywords)
	{
		List<ContentProposal> list = new ArrayList<ContentProposal>();

		List<IBizEntity> entities = project.getEntityList().match(keywords[0]);

		String strKeyword;

		if (keywords.length > 1)
		{
			strKeyword = keywords[1];
		} else
		{
			strKeyword = "";
		}

		List<String> entityKeys = new ArrayList<String>();

		for (IBizEntity entity : entities)
		{
			if (keywords.length > 1 && entity.match(keywords[0]))
			{
				entityKeys.add(entity.getId());
			}

			List<IBizSearchableNode> nodes = new ArrayList<IBizSearchableNode>();

			nodes.addAll(entity.getLogicList().match(strKeyword));
			nodes.addAll(entity.getActionList().match(strKeyword));
			nodes.addAll(entity.getAcmodeList().match(strKeyword));
			nodes.addAll(entity.getDatasetList().match(strKeyword));
			nodes.addAll(entity.getDataqueryList().match(strKeyword));
			nodes.addAll(entity.getPrintList().match(strKeyword));
			nodes.addAll(entity.getReportList().match(strKeyword));
			nodes.addAll(entity.getWfList().match(strKeyword));
			nodes.addAll(entity.getUIActionList().match(strKeyword));

			for (IBizSearchableNode mode : nodes)
			{
				for (IBizFile file : mode.getFileList())
				{
					// 判断是否有2个文件
					if (!StringKit.IsBlank(file.getUsrRealFilePath()))
					{
						list.add(new IBizSearchProposal("["+file.getProject().getCodeName()+"][USR]" + entity.getText() + " " + file.getText(), file, false));
					}
					list.add(new IBizSearchProposal("["+file.getProject().getCodeName()+"][PUB]" + entity.getText() + " " + file.getText(), file, true));
				}
			}
		}

		for (IBizApplication app : project.getAppList())
		{
			List<IBizView> views = null;
			
			if (keywords.length > 1)
			{
				views = app.getViewList().match(entityKeys,keywords);
			} else
			{
				views = app.getViewList().match(keywords);
			}

			if (views != null)
			{
				for (IBizView view : views)
				{
					String name = view.getTitle();

					for (IBizFile file : view.getFileList())
					{
						if (!StringKit.IsBlank(file.getUsrRealFilePath()))
						{
							list.add(new IBizSearchProposal("["+app.getCodeName()+"][USR]" + name + " " + file.getText(), file, false));
						}

						list.add(new IBizSearchProposal("["+app.getCodeName()+"][PUB]" + name + " " + file.getText(), file, true));
					}
				}
			}
		}

		return list;
	}
}